<template>
  <div class="bindPage">
    <!-- <div v-if="proList.length > 0"> -->
    <div v-if="true">
      <ActivitySlot class="activityBind" :imgTitle="imgTitle">
        <template #title>
          <div class="title">
            <ul>
              <li
                class="ruleItem"
                v-for="(item, index) in discountList"
                :key="index"
              >
                任选{{ item.number }}件{{ item.price }}元
              </li>
            </ul>
          </div>
        </template>
        <template #section>
          <div class="bindList" v-loading="loading">
            <div
              class="item"
              v-for="item in proList"
              :key="item.productId"
            >
              <ItemSlot :data="item" @toDetail="toProductDetail(item)">
                <template #button>
                  <button class="buy" @click="showCart(item)">加入购物车</button>
                </template>
              </ItemSlot>
            </div>
          </div>
        </template>
      </ActivitySlot>
      <el-pagination
        style="margin:20px 0;"
        background
        layout="prev, pager, next, jumper"
        :page-size="listquery.pageSize"
        :current-page="listquery.page"
        @current-change="changePage"
        :total="total" />
    </div>
    <div v-else class="noproduct">
      <svg-icon style="width: 240px; height: 240px; margin-bottom: 20px;" name="user-order-nodata" />
      <p class="fs20 font-color-999">该活动没有商品～</p>
    </div>
    <!-- 商品选择规格加入购物车弹框 -->
    <el-dialog
      title="加入购物车"
      v-model="sukVisible"
      width="800px"
    >
      <div class="productInfo" v-loading="loadingCheck">
        <img class="proImg" :src="productCheckData.image || ''" alt="" />
        <div class="proInfo">
          <div class="title">
            <div class="name">{{ productCheckData.productName }}</div>
            <div class="price" v-if="skuValues.toString().length > 0 && productCheckData.map[skuValues.toString()]">
              ￥{{ productCheckData.map[skuValues.toString()].price || 0 }}
            </div>
            <div class="price" v-if="skuValues[0] === '' && productCheckData.map">
              ￥{{ productCheckData.map['单款项'].price || 0 }}
            </div>
          </div>
          <div class="skus" v-if="productCheckData.names">
            <ul v-if="productCheckData.names.length">
              <li
                class="skusList"
                v-for="(item, index) in productCheckData.names"
                :key="item.nameCode"
              >
                <h1>{{ item.skuName }}</h1>
                <div
                  class="listItem"
                  v-for="(value, i) in item.values"
                  :key="i"
                  @click="skuValues.splice(index, 1, value.valueCode)"
                  :class="skuValues.indexOf(value.valueCode) !== -1 ? 'selected-item' : ''"
                >{{ value.skuValue }}</div>
              </li>
            </ul>
          </div>
        </div>
      </div>
      <button class="cartButton" @click="addCart">加入购物车</button>
    </el-dialog>
    <!-- 提示弹框 -->
    <el-dialog
      v-model="tipsVisible"
      width="800px"
    >
      <div class="content">
        <h1>注意事项：</h1>
        <p>1.如已添加满足条件的商品，请前往购物车结算。</p>
        <p>2.不满足规则的商品会按普通商品计算价格。</p>
        <p>3.同一订单只会计算一套规则，如满足多个规格，请拆开下单。</p>
      </div>
      <button class="cartButton" @click="tipsVisible = false">确认</button>
    </el-dialog>
  </div>
</template>

<script setup>
import { useCart } from '@/store/cart'
import ActivitySlot from '@/components/activity/activitySlot.vue'
import ItemSlot from '@/views/promotions/components/itemSlot.vue'
import {
  getBindList,
  getBindProducts,
  // getBindProduct,
  getBindDetail
} from '@/api/Activity/ActivityBind.js'
import {
  addToCart
} from '@/api/user/cart.js'
import { defineComponent, onMounted, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import SvgIcon from '@/components/svgIcon/index.vue';

defineComponent({
  name:'Rabatt'
})
const cartStore = useCart()
const imgTitle = ref('static/image/activity/bindTitle.webp')
const listquery = ref({
  priceId: null,
  page: 1,
  pageSize: 12
})
const total = ref(0)
const loading = ref(false)
const discountList = ref([])
const proList = ref([])
const sukVisible = ref(false)
const loadingCheck = ref(false)
const productCheckData = ref({})
const skuValues = ref([])
const tipsVisible = ref(false)

onMounted(()=>{
  getBindData()
})

const route = useRoute()
const router = useRouter()
// 查询定价捆绑规则
async function getBindData () {
  if (!route.query.shopId) { return }
  const response = await getBindList({ shopId: route.query.shopId })
  const res = response.data
  if (res.code === '200') {
    discountList.value.valule = res.data
  } else {
    ElMessage.error(res.message)
  }
  listquery.value.priceId = res.data[0].priceId
  getProductList()
}

// 查询定价捆绑商品
async function getProductList () {
  if (!discountList.value.length) {
    ElMessage.error('店铺信息错误！')
    return
  }
  loading.value = true
  listquery.value.ids = `${discountList.value[0].priceId}`
  listquery.value.shopId = route.query.shopId
  // const response = await getBindProduct(listquery)
  const response = await getBindProducts(listquery.value)
  // const response = await getBindProducts({
  //   ids: `${discountList[0].priceId}`,
  //   shopId: $route.query.shopId
  // })
  const res = response.data
  proList.value = res.data[0].composeProducts
  total.value = parseInt(res.data[0].total)
  loading.value = false
}
// 翻页
function changePage (val) {
  listquery.value.page = val
  getProductList()
}

// 跳转详情
function toProductDetail (row) {
  let data = {
    productId: row.productId,
    shopId: row.shopId,
    skuId: row.skuId
  }
  router.push({
    path: '/productDetail',
    query: {
      proData: JSON.stringify(data)
    }
  })
}
// 选择规格
async function showCart (item) {
  productCheckData.value = {}
  skuValues.value = []
  loadingCheck.value = true
  sukVisible.value = true
  const response = await getBindDetail({ productId: item.productId })
  const res = response.data
  productCheckData.value = res.data
  loadingCheck.value = false
}

// 添加定价商品购物车
async function addCart () {
  const getKey = skuValues.value.toString() || '单款项'
  const proNum = 1
  if (!productCheckData.value.map[getKey] || !productCheckData.value.map[getKey].skuId) {
    ElMessage.warning('请选择规格')
    return
  }
  const response = await addToCart({
    skuId: productCheckData.value.map[getKey].skuId,
    number: proNum
  })
  const res = response.data
  if (res.code === '200') {
    cartStore.setCartNumber(cartStore.cartNumber + proNum)
    tipsVisible.value = true
  } else {
    ElMessage.error('添加购物车失败！')
  }
  sukVisible.value = false
}
</script>

<style lang="scss" scoped>
.bindPage{
  max-width: 1250px;
  min-height: 600px;
  margin: auto;
  .activityBind{
    position: relative;
    .title{
      height: 35px;
      text-align: center;
      position: absolute;
      top: 160px;
      left: 0;
      right: 0;
      ul{
        display: flex;
        justify-content: center;
        align-items: center;
        .ruleItem{
          margin: 0 10px;
          padding: 0 20px;
          height: 35px;
          line-height: 35px;
          color: #FFF;
          background: linear-gradient(88deg, #D0B88E 0%, #EFD7A8 100%);
          border-radius: 25px;
          cursor: default;
        }
      }
    }
  }
  .bindList {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    .item {
      width: 23%;
      margin: 20px 1%;
      transition: all 0.3s linear;
      .progress{
        width: 100%;
        height: 40px;
        background-color: #DADBDC;
        position: absolute;
        bottom: 0;
        display: flex;
        align-items: center;
        span{
          margin-left: 10px;
        }
        :deep(.el-progress){
          flex: 1;
          height: 10px;
          margin: 0 10px;
          // .el-progress-bar{
          //   height: 10px;
          // }
          .el-progress-bar__outer,.el-progress-bar__inner{
            border-radius: 0;
            height: 10px !important;
          }
        }
      }
      .buy{
        width: 100%;
        height: 40px;
        padding: 0;
        margin: 15px 0 0;
        font-size: 18px;
        font-family: PingFang SC;
        color: #FFEBC4;
        background-color: #333333;
        cursor: pointer;
      }
    }
    .item:hover{
      box-shadow:0 0 20px #cccccc;
    }
  }
  .noproduct{
    margin: 80px 0;
    text-align: center;
  }

  .productInfo{
    display: flex;
    .proImg{
      width: 285px;
      height: 330px;
    }
    .proInfo{
      flex: 1;
      max-width: 400px;
      margin-left: 12px;
      .title{
        display: flex;
        justify-content: space-between;
        .name{
          flex: 1;
          font-size: 26px;
          font-weight: bold;
          height: 40px;
          line-height: 40px;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }
        .price{
          font-size: 20px;
          font-weight: bold;
          line-height: 40px;
          color: #C83732;
        }
      }
      .skus{
        .skusList{
          margin: 20px 0;
          .listItem{
            display: inline-block;
            height: 40px;
            line-height: 40px;
            text-align: center;
            padding: 0 9px;
            margin: 10px 8px;
            font-size: 14px;
            color: #666666;
            border: 1px solid #E4E5E6;
            cursor: pointer;
            &:hover{
              border-color: $mainGlod;
            }
          }
          .selected-item {
            color: $mainGlod;
            border-color: transparent;
            box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
          }
        }
      }
    }
  }
  .cartButton{
    display: block;
    width: 120px;
    height: 40px;
    margin: auto;
    line-height: 40px;
    text-align: center;
    color: #FFEBC4;
    background-color: #333333;
    cursor: pointer;
  }
  .content{
    max-width: 600px;
    margin: 20px auto;
    h1{
      color: #C83732;
      font-size: 26px;
    }
    p{
      margin: 8px 0;
    }
  }
}
</style>
